package org.django4j;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import com.hoursebet.common.SystemException;

public class JavaSerializationModelHandler implements ModelHandler {

    public void handle(Model model, OutputStream os) {
        Object result = model.getModel();
        if (result == null){
            throw new IllegalArgumentException("argument 'model.getModel()' can NOT be null.");
        }
        try{
            BufferedOutputStream bos = new BufferedOutputStream(os);
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(result);
            oos.close();
        }
        catch(IOException e){
            throw new SystemException(e);
        }
    }

}
